import com.sun.source.tree.Tree;

import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;//存储左孩子的引用
        public TreeNode right;//存储右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    public int i = 0;
    public TreeNode createTree(String str){
        TreeNode root = new TreeNode(str.charAt(i));
        if(str.charAt(i) != '#'){

            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        }else{
            i++;
            return null;
        }
        return root;
    }


    // 前序遍历
    public void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        System.out.print(root.val + " ");
        preOrder((root.left));
        preOrder((root.right));
    }
    // 中序遍历
    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }

        inOrder((root.left));
        System.out.print(root.val + " ");
        inOrder((root.right));
    }
    // 后序遍历
    public void postOrder(TreeNode root){
        if(root == null){
            return;
        }

        postOrder((root.left));
        postOrder((root.right));
        System.out.print(root.val + " ");
    }


    public static int nodeSize = 0;
    // 获取树中节点的个数
    public int size(TreeNode root){
        if(root == null){
            return -1;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
        return nodeSize;
    }
    public int size2(TreeNode root){
        if(root == null){
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }

    public static int leafSize = 0;
    public void getLeafNodeSize(TreeNode root){
        if(root == null){
            return;
        }
        if(root.left == null && root.right == null){
            leafSize++;
        }
        getLeafNodeSize(root.left);
        getLeafNodeSize(root.right);
    }

    public int getLeafNodeSize2(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }

        return getLeafNodeSize2(root.left) + getLeafNodeSize2(root.right);
    }

    public int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }

        return getKLevelNodeCount(root.left,k - 1) + getKLevelNodeCount(root.right,k - 1);
    }



    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, int val){
        if(root.val == val){
            return root;
        }
        TreeNode left = find(root.left,val);
        if(left != null){
            return left;
        }
        TreeNode right = find(root.right,val);
        if(right!= null){
            return right;
        }
        return null;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {

        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null){
            return ;
        }
        queue.offer(root);

        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right != null){
                queue.offer(cur.right);
            }

        }
        System.out.println();
    }

    //层序遍历2
    public List<List<Character>> levelOrder2(TreeNode root) {
        List<List<Character>> ret = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null){
            return null;
        }
        queue.offer(root);

        while(!queue.isEmpty()){
            List<Character> list = new ArrayList<>();
            int size = queue.size();
            while(size != 0) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }

    //判断是否是完全二叉树（依旧层序遍历）

    public boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null){
            return true;
        }
        queue.offer(root);

        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                while(!queue.isEmpty()){
                    TreeNode check = queue.poll();
                    if(check != null){
                        return false;
                    }
                }
            }
        }
        return true;
    }

    //翻转二叉树
    public TreeNode invertTree(TreeNode root) {

        if(root == null){
            return null;
        }
        if(root.right == null && root.left == null){
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }


    //检查两棵树是否相同
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null){
            return true;
        }
        if(p == null && q != null || p != null && q == null){
            return false;
        }
        if(p.val == q.val){
            boolean leftRet = isSameTree(p.left,q.left);
            if(!leftRet){
                return false;
            }
            boolean rightRet  = isSameTree(p.right,q.right);
            if(!rightRet){
                return false;
            }
            return true;
        }
        return false;
    }

    //对称二叉树
    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }
        return isSymmetricChild(root.left,root.right);

    }
    public boolean isSymmetricChild(TreeNode p,TreeNode q){
        if(p == null && q == null){
            return true;
        }
        if(p == null && q != null || p != null && q == null){
            return false;
        }
        if(p.val == q.val){
            boolean leftRet = isSymmetricChild(p.left,q.right);
            if(!leftRet){
                return false;
            }
            boolean rightRet  = isSymmetricChild(p.right,q.left);
            if(!rightRet){
                return false;
            }
            return true;
        }
        return false;
    }

    //另一颗树的子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null){
            return false;
        }
        if(root.val == subRoot.val){
            if (isSameTree(root,subRoot)){
                return true;
            }else{
                if(isSubtree(root.left,subRoot)){
                    return true;
                }
                if(isSubtree(root.right,subRoot)){
                    return true;
                }
            }
        }
        if(isSubtree(root.left,subRoot)){
            return true;
        }
        if(isSubtree(root.right,subRoot)){
            return true;
        }
        return false;
    }


    //平衡二叉树
    /*
    * 时间复杂度O(n^2)
    * */
    public boolean isBalanced(TreeNode root) {
        if(root == null){
            return true;
        }
        if(!isBalanced(root.left)){
            return false;
        }

        if(!isBalanced(root.right)){
            return false;
        }


        int h = getHeight(root.left) - getHeight(root.right);
        if(h < -1 || h > 1){
            return  false;
        }else{
            return true;
        }

    }
    // 获取二叉树的高度
    public int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }

        return Math.max(getHeight(root.left),getHeight(root.right)) + 1;
    }

    //平衡二叉树
    /*
     * 时间复杂度O(n)
     * */

    public boolean isBalanced2(TreeNode root){
        if(root == null){
            return true;
        }
        return getHeight2(root) >=0;
    }
    public int getHeight2(TreeNode root){
        if(root == null){
            return 0;
        }
        int left = getHeight2(root.left);
        if(left == -1){
            return -1;
        }
        int right = getHeight2(root.right);

        if(right == -1 || Math.abs(left - right) > 1){
            return -1;
        }else{
            return Math.max(left,right) + 1;
        }
    }








    //最近的祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);
        if(root == p || root == q){
            return root;
        }
        if(left != null && right != null){
            return root;
        }else if(left != null){
            return left;
        }else{
            return right;
        }
    }

    public boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack){
        if(root == null){
            return false;
        }
        stack.push(root);
        if(root != node){
            boolean ret = getPath(root.left,node,stack);
            if(ret){
                return true;
            }
            ret = getPath(root.right,node,stack);
            if(ret){
                return true;
            }
        }else{
            return true;
        }
        stack.pop();
        return false;
    }

    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q){
        if(root == null){
            return null;
        }
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();
        getPath(root,p,stackP);
        getPath(root,q,stackQ);

        int n = stackP.size() - stackQ.size();
        if(n > 0){
            while(n > 0){
                stackP.pop();
                n--;
            }
        }else{
            n = -n;
            while(n > 0){
                stackQ.pop();
                n--;

            }
        }
        TreeNode ret = null;
        while(!stackQ.isEmpty() && !stackP.isEmpty()){
            if(stackQ.peek() == stackP.peek()){
                ret = stackQ.peek();
                break;
            }else{
                stackQ.pop();
                stackP.pop();
            }
        }
        return ret;
    }



    public List<List<Character>> levelOrderBottom(TreeNode root) {
        if(root == null){
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();

        queue.offer(root);
        Stack<List<Character>> stack = new Stack<>();

        while(!queue.isEmpty()){
            List<Character> arrayList = new ArrayList<>();
            int size = queue.size();
            while(size != 0){
                TreeNode cur = queue.poll();
                arrayList.add(cur.val);
                if(cur.left != null){
                    queue.add(cur.left);

                }
                if(cur.right != null){
                    queue.add(cur.right);
                }

                size--;
            }
            stack.push(arrayList);
        }

        List<List<Character>> result = new ArrayList<>();
        while(!stack.isEmpty()){
            result.add(stack.pop());
        }
        return result;
    }


    //根据二叉树创建字符串
    public String tree2str(TreeNode root) {
        if(root == null){
            return null;
        }
        StringBuilder stringbuilder = new StringBuilder();
        tree2strchild(root,stringbuilder);
        return stringbuilder.toString();
    }
    public void tree2strchild(TreeNode root,StringBuilder stringbuilder){
        if(root == null){
            return;
        }
        stringbuilder.append(root.val);
        if(root.left != null){
            stringbuilder.append("(");
            tree2strchild(root.left,stringbuilder);
            stringbuilder.append(")");
        }else{
            if(root.right == null){
                return;
            }else{
                stringbuilder.append("()");
            }
        }
        if(root.right != null){
            stringbuilder.append("(");
            tree2strchild(root.right,stringbuilder);
            stringbuilder.append(")");
        }else{
            return;
        }

    }


}














